package com.csq.androidproject.ui.activitys;

import hirondelle.date4j.DateTime;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.csq.androidproject.R;
import com.csq.androidproject.utils.DateUtils;
import com.csq.androidproject.views.calendarview.caldroid.SpecialCalendarGridCell;
import com.csq.androidproject.views.calendarview.caldroid.listeners.CaldroidListener;
import com.csq.androidproject.views.calendarview.caldroid.views.CalendarView;

public class CalendarActivity extends Activity {
    
    CalendarView calendarView;
    
    private HashMap<String, DateTime> greenDates;
    private HashMap<String, DateTime> redDates;
    private HashMap<String, DateTime> yellowDates;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calendar);
        calendarView = (CalendarView)findViewById(R.id.calendarView);
        
        test();
    }
    
    public void onClick(View v){
        switch (v.getId()) {
            case R.id.btnToToday:
                calendarView.moveToDateTime(DateUtils.getCurrentDataTime());
                break;
            
            default:
                break;
        }
    }
    
    
    private void test(){
        
        DateTime today = DateUtils.getCurrentDataTime();
        DateTime tmp;
        
        greenDates = new HashMap<String, DateTime>();
        tmp = today.minusDays(4);
        greenDates.put(DateUtils.getYMDString(tmp),tmp);
        tmp = today.minusDays(4);
        greenDates.put(DateUtils.getYMDString(tmp),tmp);
        tmp = today.plusDays(4);
        greenDates.put(DateUtils.getYMDString(tmp),tmp);
        tmp = today.plusDays(30);
        greenDates.put(DateUtils.getYMDString(tmp),tmp);
        
        redDates = new HashMap<String, DateTime>();
        tmp = today.minusDays(6);
        redDates.put(DateUtils.getYMDString(tmp),tmp);
        tmp = today.minusDays(6);
        redDates.put(DateUtils.getYMDString(tmp),tmp);
        tmp = today.plusDays(6);
        redDates.put(DateUtils.getYMDString(tmp),tmp);
        tmp = today.plusDays(26);
        redDates.put(DateUtils.getYMDString(tmp),tmp);
        
        yellowDates = new HashMap<String, DateTime>();
        tmp = today.minusDays(2);
        yellowDates.put(DateUtils.getYMDString(tmp),tmp);
        tmp = today.minusDays(9);
        yellowDates.put(DateUtils.getYMDString(tmp),tmp);
        tmp = today.plusDays(9);
        yellowDates.put(DateUtils.getYMDString(tmp),tmp);
        tmp = today.plusDays(35);
        yellowDates.put(DateUtils.getYMDString(tmp),tmp);
        
        List<SpecialCalendarGridCell>specials = new ArrayList<SpecialCalendarGridCell>();
        for(DateTime dt : greenDates.values()){
            specials.add(new SpecialCalendarGridCell(dt, 
                R.drawable.calendar_cell_bg_green, 
                Color.BLACK));
        }
        
        for(DateTime dt : redDates.values()){
            specials.add(new SpecialCalendarGridCell(dt, 
                R.drawable.calendar_cell_bg_red, 
                Color.BLACK));
        }
        
        for(DateTime dt : yellowDates.values()){
            specials.add(new SpecialCalendarGridCell(dt, 
                R.drawable.calendar_cell_bg_yellow, 
                Color.BLACK));
        }
        
        calendarView.setSpecialDates(specials);
        
        calendarView.setCaldroidListener(new CaldroidListener() {

            @Override
            public void onChangeMonth(int month, int year) {
            }

            @Override
            public void onCaldroidViewCreated() {
                Toast.makeText(getApplicationContext(),
                    "Caldroid view is created", Toast.LENGTH_SHORT)
                    .show();
            }

            @Override
            public void onClicked(DateTime date, View view) {
                // TODO Auto-generated method stub
                String dataStr = DateUtils.getYMDString(date);
                if(greenDates.containsKey(dataStr)){
                    Toast.makeText(getApplicationContext(),
                        "green clicked : "+dataStr, 
                        Toast.LENGTH_SHORT)
                        .show();
                }else if(redDates.containsKey(dataStr)){
                    Toast.makeText(getApplicationContext(),
                        "red clicked : "+dataStr, 
                        Toast.LENGTH_SHORT)
                        .show();
                }else if(yellowDates.containsKey(dataStr)){
                    Toast.makeText(getApplicationContext(),
                        "yellow clicked : "+dataStr, 
                        Toast.LENGTH_SHORT)
                        .show();
                }else{
                    Toast.makeText(getApplicationContext(),
                        "normal clicked : "+dataStr, 
                        Toast.LENGTH_SHORT)
                        .show();
                }
            }

            @Override
            public void onLongClicked(DateTime date, View view) {
                // TODO Auto-generated method stub
                String dataStr = DateUtils.getYMDString(date);
                if(greenDates.containsKey(dataStr)){
                    Toast.makeText(getApplicationContext(),
                        "green longclicked : "+dataStr, 
                        Toast.LENGTH_SHORT)
                        .show();
                }else if(redDates.containsKey(dataStr)){
                    Toast.makeText(getApplicationContext(),
                        "red longclicked : "+dataStr, 
                        Toast.LENGTH_SHORT)
                        .show();
                }else if(yellowDates.containsKey(dataStr)){
                    Toast.makeText(getApplicationContext(),
                        "yellow longclicked : "+dataStr, 
                        Toast.LENGTH_SHORT)
                        .show();
                }else{
                    Toast.makeText(getApplicationContext(),
                        "normal longclicked : "+dataStr, 
                        Toast.LENGTH_SHORT)
                        .show();
                }
            }
        });
    }
    
}
